package com.playball.lottery.frm.ads;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import com.playball.lottery.R;
import com.playball.lottery.ApplicationData;
import com.playball.lottery.frm.yhzx.JddRegisterActivity.loginAsyncLoader;
import com.playball.lottery.nt.AppVersionConfig;
import com.playball.lottery.nt.GetRequestParams;
import com.playball.lottery.util.AbstractActivity;
import com.playball.lottery.util.AppUtils;
import com.playball.lottery.util.ConvertJsonToList;

import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 公告  详情
* @ClassName: AdvWebviewDetailActivity 
* @Description: TODO(这里用一句话描述这个类的作用) 
* @author vanszhen 
* @date 2014年6月6日 上午10:24:39 
*
 */
public class AdvWebviewDetailActivity extends AbstractActivity  implements OnClickListener{
	
	
	/**WebView **/
	private WebView mWebView ;
	/**返回*/
	private TextView top_title_back ;
	/**标题*/
	private TextView top_title_info;
	/**String url*/
	//private String url ;
	/**标题*/
	private String title ;
	
	private String tid ;
	
	private String[] parms;
	
	/** 网络获取的数据 */
	private 	List<Map<String, Object>> netDataList;
	
    Handler handler = new Handler(){
		
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case 0X001:
//				mWebView.loadData(netDataList.get(0).get("Content").toString(),  "text/html; charset=UTF-8", null);
				mWebView.loadDataWithBaseURL(null, netDataList.get(0).get("Content").toString(), "text/html", "utf-8", null);
				break;
			case 0X002:
				break;
			default:
				break;
			}
			
		};
		
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.adv_web_detail);
		initView();
		initData();
		initParames();
		initWebView();
	}




	/*初始化view*/
	private void initView() {
		top_title_back = (TextView) findViewById(R.id.top_title_back);
		top_title_back.setOnClickListener(this);
		top_title_info = (TextView) findViewById(R.id.top_title_info);
		mWebView = (WebView) findViewById(R.id.adv_webView);
	}
	
	/**初始化数据*/
	private void initData() {
		//url = getIntent().getStringExtra("url");
		tid = getIntent().getStringExtra("id");
		title = getIntent().getStringExtra("title");
		top_title_info.setText(title);
	}

	/*初始化参数*/
	private void initParames(){
	     parms = new String[2];
		 JSONObject params = new JSONObject();
		 try {
		 // 设置请求所需参数
			 params.put("CmdID", AppUtils.getplatformTypeByCmdId(ApplicationData.getInstance().getCommenderID()));
			 params.put("Count", AppVersionConfig.NEWS_COUNT);
			 params.put("NewID", tid);
		 } catch (JSONException ex) {
		 Log.e("error", ex.toString());
		 }
		parms[1] = params.toString();
		parms[0] = "22";
		asyLoad();
	}

	/***初始化webview*/
	private void initWebView() {
		// TODO Auto-generated method stub
		
		mWebView.getSettings().setJavaScriptEnabled(true);
	/*	mWebView.getSettings().setSupportZoom(true);
		mWebView.getSettings().setDomStorageEnabled(true);
		mWebView.getSettings().setAllowFileAccess(true);
		mWebView.getSettings().setUseWideViewPort(true);
		mWebView.getSettings().setBuiltInZoomControls(true);
		mWebView.requestFocus();
		mWebView.getSettings().setLoadWithOverviewMode(true);*/
		mWebView.getSettings().setDefaultTextEncodingName("UTF-8") ;
		
		
		mWebView.setWebViewClient(new WebViewClient() {
			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				view.loadUrl(url);
				return true;
			}
		});

		
	}

	
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.top_title_back:
			AdvWebviewDetailActivity.this.finish();
			break;

		default:
			break;
		}
	}
	
	private void asyLoad(){
		AsyLoadAdv adv = new AsyLoadAdv();
		adv.execute(parms);
	}
	

	/**
	 * 异步加载
	* @ClassName: AsyLoadAdv 
	* @Description: TODO(这里用一句话描述这个类的作用) 
	* @author vanszhen 
	* @date 2014年6月5日 下午3:57:42 
	*
	 */
	class AsyLoadAdv extends AsyncTask<String, Integer, String>{

		@Override
		protected String doInBackground(String... params) {
			String strResult;
			String action = params[0];
			strResult = GetRequestParams.getRequestUserIDResult(params[1], ApplicationData.getInstance().getUserID(), action, getApplicationContext());
			return strResult;
		}
		
		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			try {
				if (result != null) {
					JSONTokener jsonParser = new JSONTokener(result);
					JSONObject js = (JSONObject) jsonParser.nextValue();
					if (Integer.parseInt(js.getString("Code")) == 0) {
						netDataList = getData(result);
						if(netDataList != null){
							Log.d("van", netDataList.toString());
							handler.sendEmptyMessage(0X001);
						}
						//Log.d("van", netDataList.toString());
					}
				}else {
					handler.sendEmptyMessage(0X002);
				}
			} catch (Exception e) {
//				e.printStackTrace();
			}
			
		}
		
	};
	
	
	/**
	 * 网络结果数据json转list
	 * 
	 * @param strResult
	 *            网络结果数据json
	 * @return List
	 */
	private 	List<Map<String, Object>> getData(String strResult) {
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		try {
			JSONTokener jsonParser = new JSONTokener(strResult);
			JSONObject js = (JSONObject) jsonParser.nextValue();
			int code = Integer.parseInt(js.getString("Code"));
			if (code <= -1) {
				String msg = js.getString("Data");
				Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
			} else {
				//String str  = js.getString("Content");
			    list = ConvertJsonToList.getM(js.getString("Data"));
			  //  Log.d("van", list.toString());
			}
		} catch (Exception ex) {
//			ex.printStackTrace();
		}
		return list;
	}
}
